package com.example.administrator.videotest2;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.videotest2.util.Search_viewActivity;
import com.example.administrator.videotest2.util.Tab.Constant;
import com.umeng.analytics.MobclickAgent;

import java.io.File;

public class Index_MainActivity extends TabActivity {


    //  private FragmentManager manager = getSupportFragmentManager();


    private long exitTime = 0L;
    private TabHost mTabHost;
    private LayoutInflater mInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_index__main);
        initView();
    }

    /**
     * 显示菜单资源文件
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = new MenuInflater(Index_MainActivity.this);
        inflater.inflate(R.menu.action_bar_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }


    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.search_1) {
            Intent search_intent = new Intent(Index_MainActivity.this, Search_viewActivity.class);
            startActivity(search_intent);
            // search();
        /*switch (item.getItemId()){
            case 0: search();
                Toast.makeText(Index_MainActivity.this,"789987",Toast.LENGTH_SHORT).show();
                break;
            case 1:Toast.makeText(Index_MainActivity.this,"789987",Toast.LENGTH_SHORT).show();
                break;
            default:break;*/
        }
        return true;
    }

    /**
     * 初始化组件
     */
    private void initView() {
        mTabHost = getTabHost();
        mInflater = getLayoutInflater().from(this);

        int count = Constant.ConValue.mTabClassArray.length;

        for (int i = 0; i < count; i++) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(Constant.ConValue.mTextViewArray[i]).setIndicator(getTabItemView(i)).setContent(getTabItemIntent(i));

            mTabHost.addTab(tabSpec);
        }

    }

    private Intent getTabItemIntent(int i) {

        Intent intent = new Intent(this, Constant.ConValue.mTabClassArray[i]);
        return intent;
    }


    private View getTabItemView(int i) {

        View view = mInflater.inflate(R.layout.item_tab, null);
        ImageView imageView = view.findViewById(R.id.image_icon);
        if (imageView != null) {
            imageView.setImageResource(Constant.ConValue.mImageViewArray[i]);
        }
        TextView textView = view.findViewById(R.id.text_name);
        textView.setText(Constant.ConValue.mTextViewArray[i]);
        return view;
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getRepeatCount() == 0) {
                if (System.currentTimeMillis() - exitTime > 2000) {
                    Toast.makeText(this, "再按一次退出", Toast.LENGTH_LONG).show();
                    exitTime = System.currentTimeMillis();
                } else {
                    this.finish();
                    System.exit(0);
                }
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        cleanSharedPreference(this.getBaseContext());
        cleanInternalCache(this.getBaseContext());
    }

    /**
     * * 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) * * @param
     * context
     */
    public static void cleanSharedPreference(Context context) {
        deleteFilesByDirectory(new File("/data/data/"
                + context.getPackageName() + "/shared_prefs"));
    }


    /** * 删除方法 这里只会删除某个文件夹下的文件，如果传入的directory是个文件，将不做处理 * * @param directory */
    private static void deleteFilesByDirectory(File directory) {
        if (directory != null && directory.exists() && directory.isDirectory()) {
            for (File item : directory.listFiles()) {
                item.delete();
            }
        }
    }

    public static void cleanInternalCache(Context context) {
        deleteFilesByDirectory(context.getCacheDir());
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }

}